﻿using System;
using System.Data;
using Navigant.Infrastructure.Cases;
using Navigant.Infrastructure.EntityFactoryFramework;
using Navigant.Infrastructure.Helpers;
using Navigant.Model.Custodians;

namespace Navigant.Infrastructure.Repositories
{
    public class CustodianFactory : IEntityFactory<Custodian>
    {
        #region Field Names

        internal static class FieldNames
        {
            public const string Id = "ID";
            public const string Name = "Title";
            public const string CustodianName = "Custodian Name";
            public const string Email = "Email";
            public const string IsPerson = "Is_x0020_Person";
            public const string IsPeople = "Is_x0020_People";
            public const string IsInternal = "Is_x0020_Internal";
            public const string LeaveReturnDate = "LV_x002d_Return_x0020_Date";
            public const string LeaveAlertAddress = "LV_x002d_Alert_x0020_Address";
            public const string LeaveAlertDate= "LV_x002d_Alert_x0020_Date";

            public const string LeaveReturnDateDisplayName = "LV-Return Date";
            public const string LeaveAlertAddressDisplayName = "LV-Alert Address";
            public const string LeaveAlertDateDisplayName = "LV-Alert Date";
        }

        #endregion

        #region IEntityFactory<Custodian> Members

        public Custodian BuildEntity(IDataReader reader, ICase parentCase)
        {
            Custodian custodian = new Custodian(parentCase,
                new CustodianName(DataHelper.GetString(reader[FieldNames.Name])),
                DataHelper.GetString(reader[FieldNames.Email]), 
                DataHelper.GetInteger(reader[FieldNames.Id]));

            custodian.IsInternal = DataHelper.GetBoolean(reader[FieldNames.IsInternal]);

            // Get the schema for the reader in order to test 
            // if certain fields are present or not
            DataTable schema = reader.GetSchemaTable();

            if (DataHelper.ReaderContainsColumnName(schema, FieldNames.IsPerson))
            {
                custodian.IsPerson = DataHelper.GetBoolean(reader[FieldNames.IsPerson]);
            }
            else if (DataHelper.ReaderContainsColumnName(schema, FieldNames.IsPeople))
            {
                custodian.IsPerson = DataHelper.GetBoolean(reader[FieldNames.IsPeople]);
            }
            else
            {
                custodian.IsPerson = true;
            }

            // See if Leave Return Date is present
            if (DataHelper.ReaderContainsColumnName(schema, FieldNames.LeaveReturnDate))
            {
                // It is, so get and set the value
                DateTime? leaveReturnDate = DataHelper.GetNullableDateTime(reader[FieldNames.LeaveReturnDate]);
                if (leaveReturnDate.HasValue)
                {
                    custodian.SetExtendedProperty(FieldNames.LeaveReturnDateDisplayName, 
                        leaveReturnDate.Value.ToString());
                }
            }

            // See if Leave Alert Date is present
            if (DataHelper.ReaderContainsColumnName(schema, FieldNames.LeaveAlertDate))
            {
                // It is, so get and set the value
                DateTime? leaveAlertDate = DataHelper.GetNullableDateTime(reader[FieldNames.LeaveAlertDate]);
                if (leaveAlertDate.HasValue)
                {
                    custodian.SetExtendedProperty(FieldNames.LeaveAlertDateDisplayName, 
                        leaveAlertDate.Value.ToString());
                }
            }

            // See if Leave Alert Address is present
            if (DataHelper.ReaderContainsColumnName(schema, FieldNames.LeaveAlertAddress))
            {
                // It is, so get and set the value
                string leaveAlertAddress = DataHelper.GetString(reader[FieldNames.LeaveAlertAddress]);
                if (!string.IsNullOrEmpty(leaveAlertAddress))
                {
                    custodian.SetExtendedProperty(FieldNames.LeaveAlertAddressDisplayName,
                        leaveAlertAddress);
                }
            }
       
            return custodian;
        }

        public void LoadExtendedProperties(IDataReader reader, Custodian entity)
        {
            if (reader != null)
            {
                DataTable schema = reader.GetSchemaTable();
                string columnName = string.Empty;
                foreach (DataRow row in schema.Rows)
                {
                    columnName = row["ColumnName"].ToString();
                    entity.SetExtendedProperty(columnName, reader[columnName].ToString(), true);
                }
            }
        }

        #endregion
    }
}
